<template>
  <div class="pass">
    <!-- 密切接触者管理 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>密切接触者管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <div class="sousuo">
        <div class="search">
          <!--两个按钮 -->
          <el-button type="info" @click="getTouchAdd">新建</el-button>
          <el-button type="info" @click="doRefresh">刷新</el-button>
        </div>
        <div class="search">
          <!-- 一个输入框，两个按钮 -->
          <el-input v-model="searchById" placeholder="请输入ID" />
          <el-button type="primary" @click="getTouchById">搜索</el-button>
        </div>
      </div>

      <!-- 表格 -->
      <el-table :data="touchList">
        <el-table-column prop="touch.id" label="ID"></el-table-column>
        <el-table-column prop="touch.baseId" label="baseID"></el-table-column>
        <el-table-column prop="safeBase.uname" label="姓名"></el-table-column>
        <!-- 快速复制某行 alt+shift+剪头 -->
        <el-table-column
          prop="safeBase.idCard"
          label="身份证号"
        ></el-table-column>
        <el-table-column prop="safeBase.gender" label="性别"></el-table-column>
        <el-table-column
          prop="touch.comeFrom"
          label="接触来源"
        ></el-table-column>
        <el-table-column
          prop="touch.isoAddress"
          label="隔离地址"
        ></el-table-column>
        <el-table-column
          prop="touch.desc1"
          label="症状"
        ></el-table-column>
        <el-table-column
          prop="touch.startDate"
          label="隔离开始日期"
        ></el-table-column>

        <el-table-column label="当前状态">
          <!-- 可以通过scope.row 拿到当前这一行数据 -->
          <template v-slot="scope">
            <!-- 0 待审核 1通过 2不通过 -->
            <el-tag v-if="scope.row.touch.finished == '完成隔离'" type="success"
              >完成隔离</el-tag
            >
            <el-tag
              v-if="scope.row.touch.finished == '未完成隔离'"
              type="danger"
              >未完成隔离</el-tag
            >
          </template>
        </el-table-column>
        <!-- 只有当状态为待审核，也就是0时，才展示两个按钮 -->
        <el-table-column label="操作" width="400px">
          <template v-slot="scope">
            <div class="operation">
              <el-button
                color="green"
                @click="
                  goToUpdate(
                    scope.row.touch.id,
                    scope.row.touch.comeFrom,
                    scope.row.touch.isoAddress,
                    scope.row.touch.startDate,
                    scope.row.touch.finished,
                    scope.row.safeBase.id,
                    scope.row.safeBase.uname,
                    scope.row.safeBase.idCard,
                    scope.row.safeBase.gender,
                    scope.row.touch.desc1,
                    
                  )
                "
                >编辑</el-button
              >
              <el-button color="red" @click="deleteTouch(scope.row.touch.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="paging"
        background
        layout="prev, pager, next"
        :total="1000"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
// getAllPassAPi获取所有通行证 发请求
import { ElMessage } from "element-plus";
import {
  getAllPassAPi,
  deletePassApi,
  changePassStatusApi,
  getUserPassApi,
} from "../api/index.js";
import { getAllTouchAPi, getTouchByIdApi,deleteTouchApi } from "../api/index1.js";
export default {
  inject: ["refresh"],
  data() {
    return {
      touchList: [],
      searchById: "",
    };
  },
  methods: {
    //更新的方法
    goToUpdate(touchId,comeFrom,isoAddress,startDate,finished,baseId,uname,idCard,gender,desc) {
      this.$router.push({
        name: "touchEdit", //要跳转的页面的路由
        params:{
          touchId:touchId,
          comeFrom:comeFrom,
          isoAddress:isoAddress,
          startDate:startDate,
          finished:finished,
          baseId:baseId,
          uname:uname,
          idCard:idCard,
          gender:gender,
          desc:desc
        }
      });
    },
    //刷新的方法
    doRefresh() {
      this.refresh();
    },
    getTouchAdd() {
      this.$router.push({
        path: "../views/touchAdd", //要跳转的页面的路由
      });
    },
    // 1.获取密接者列表的方法
    getTouchList() {
      getAllTouchAPi().then((res) => {
        console.log(res);
        this.touchList = res.data;
      });
    },
    // 2.根据id删除密接者
    deleteTouch(id) {
      deleteTouchApi(id).then((res) => {
        console.log(res);
        // 1.刷新数据
        this.getTouchList();
        // 2.提示用户删除成功
        if (res.code == 20000) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 3.改变通行证状态
    changeStatus(id, status) {
      console.log(id + status);

      let obj = {
        id: id,
        status: status,
      };
      changePassStatusApi(obj).then((res) => {
        console.log(res);
        if (res.code == 20000) {
          this.getPassList();
          ElMessage({
            message: "更新成功",
            type: "success",
          });
        }
      });
    },
    // 4.搜索
    getTouchById() {
      getTouchByIdApi(this.searchById).then((res) => {
        console.log(res);
        this.touchList = res.data;
      });
    },
    // 5.重置
    reset() {
      this.searchById = "";
      this.getPassList();
    },
  },
  created() {
    // this.getPassList();
    this.getTouchList();
  },
};
</script>

<style lang="less" scoped>
.pass {
  .el-breadcrumb {
    margin-bottom: 30px;
  }
}
.search {
  width: 300px;
  display: flex;
  .el-input {
    margin-right: 30px;
  }
}
.operation {
  display: flex;
}

.sousuo {
  display: flex;
  justify-content: space-between;
}
.paging {
  display: flex;
  justify-content: right;
}
</style>